package com.android.fitcontrol;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.android.fitcontrol.bean.Training;
import com.android.fitcontrol.common.CommonUtils;
import com.android.fitcontrol.database.DBConnector;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;

public class MonthScheduleActivity extends Activity {
	
	private CalendarView mCalendar;
	
	private DBConnector connector;
	
	@Override 
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		setContentView(R.layout.activity_month_schedule);
		
		ActionBar actionBar = getActionBar();
		connector = new DBConnector(this);
		
//		if(getIntent().getExtras() == null || getIntent().getExtras().getBoolean("isUp")) {
//			actionBar.setDisplayHomeAsUpEnabled(true);
//		}
		
		mCalendar = (CalendarView) findViewById(R.id.month_calendar);
		mCalendar.setOnDateChangeListener(new OnDateChangeListener() {
			
			public void onSelectedDayChange(CalendarView calendar, int year, int month,
					int dayOfMonth) {
				try {
					
					Format formatter = new SimpleDateFormat("dd-MM-yyyy");
					String date =  dayOfMonth + "-" + (month + 1) + "-" + year;
					final Date resultDate = (Date) formatter.parseObject(date);
					
					final Intent exerciseListIntent = new Intent(MonthScheduleActivity.this, ExerciseListActivity.class);
					exerciseListIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					exerciseListIntent.putExtra("pickedDate", resultDate.getTime());
					String selection = Training.USER_ID + " = ? and " + Training.DATE + " = ?";
					String [] args = new String[]{String.valueOf(CommonUtils.getUserId()), String.valueOf(resultDate.getTime())};
					Cursor c = connector.query(Training.TABLE_NAME, null, selection, args, null, null, null);
					boolean isExistTraining = false;
					if(c.moveToFirst()){
						isExistTraining = true;
						exerciseListIntent.putExtra("training_id", c.getInt(Training.NUM_ID));
					}
					c.close();
									
					if(!isExistTraining) {
						AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MonthScheduleActivity.this);
						alertBuilder.setTitle(R.string.schedule_week_dialog_title).setNegativeButton("Cancel", new OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								
							}
						}).setPositiveButton("Create", new OnClickListener() {
							
							public void onClick(DialogInterface dialog, int which) {
								Training tra = new Training();
								tra.setDate(resultDate.getTime());
								tra.setUser_ID(CommonUtils.getUserId());
								connector.insert(tra);
								String selection = Training.USER_ID + " = " + CommonUtils.getUserId() + " and " + Training.DATE + " = " + resultDate.getTime();
								Cursor c = connector.query(Training.TABLE_NAME, null, selection, null, null, null, null);
								c.moveToFirst();
								exerciseListIntent.putExtra("training_id", c.getInt(Training.NUM_ID));
								c.close();
								startActivity(exerciseListIntent);
							}
						}).setMessage("Do you want to create a new complex?").show();
					} else {
						startActivity(exerciseListIntent);
					}
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		connector.close();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case android.R.id.home:
			Intent backIntent = new Intent(this, ExerciseListActivity.class);
			backIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(backIntent);
			break;
		case R.id.action_bar_logout_item:
			connector.delete("user", -1);
			connector.close();
			Intent loginIntent = new Intent(this, LoginActivity.class);
			loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(loginIntent);
			finish();
			break;
		}
		return true;
	}
	
	public void onDataPicked(View view) {
		
	}
}